package com.ruoyi.web.controller.comlbs;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.SnowFlakeIDGenerator;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.domain.PhotoWallConfig;
import com.ruoyi.system.service.IPhotoWallConfigService;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.List;

/**
 * 照片墙行列配置Controller
 * 
 * @author wp
 * @date 2023-08-10
 */
@RestController
@RequestMapping("/comlbs/photoWallConfig")
public class PhotoWallConfigController extends BaseController {
    @Autowired
    private IPhotoWallConfigService photoWallConfigService;

    /**
     * 查询照片墙行列配置列表
     */
    @GetMapping("/list")
    public TableDataInfo list(PhotoWallConfig photoWallConfig) {
        startPage();
        List<PhotoWallConfig> list = photoWallConfigService.selectList(photoWallConfig);
        return getDataTable(list);
    }

    /**
     * 导出照片墙行列配置列表
     */
    @Log(title = "照片墙行列配置", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, PhotoWallConfig photoWallConfig) {
        List<PhotoWallConfig> list = photoWallConfigService.selectList(photoWallConfig);
        ExcelUtil<PhotoWallConfig> util = new ExcelUtil<PhotoWallConfig>(PhotoWallConfig.class);
        util.exportExcel(response, list, "照片墙行列配置数据");
    }

    /**
     * 获取照片墙行列配置详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(photoWallConfigService.getById(id));
    }

    /**
     * 新增照片墙行列配置
     */
    @Log(title = "照片墙行列配置", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody PhotoWallConfig photoWallConfig) {
        return toAjax(photoWallConfigService.save(photoWallConfig));
    }

    /**
     * 修改照片墙行列配置
     */
    @Log(title = "照片墙行列配置", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody PhotoWallConfig photoWallConfig) {
        return toAjax(photoWallConfigService.updateById(photoWallConfig));
    }

    /**
     * 删除照片墙行列配置
     */
    @Log(title = "照片墙行列配置", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(photoWallConfigService.removeByIds(Arrays.asList(ids)));
    }

    @ApiOperation("获取配置")
    @PostMapping(value = "/getConfig")
    public AjaxResult getConfig() {
        List<PhotoWallConfig> configList = photoWallConfigService.selectList(new PhotoWallConfig(getUserId()));
        return success(ObjectUtils.isEmpty(configList) ? new PhotoWallConfig(3, 8) : configList.get(0));
    }

    @ApiOperation("保存配置")
    @ApiOperationSupport(includeParameters = {"row","col"})
    @PostMapping("/saveConfig")
    public AjaxResult save(@RequestBody PhotoWallConfig param) {
        PhotoWallConfig photoWallConfig = photoWallConfigService.getOne(new QueryWrapper<PhotoWallConfig>().eq("create_by", getUserId()));
        if (photoWallConfig == null) photoWallConfig = new PhotoWallConfig();
        photoWallConfig.setRow(param.getRow()).setCol(param.getCol());
        photoWallConfigService.saveOrUpdate(photoWallConfig);
        return success();
    }
}
